package com.aaron.study.customer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.aaron.study.common.entity.TextMessage;
import com.aaron.study.customer.feign.KeFuFeignClient;

import lombok.extern.java.Log;

/**
 * 接收来自客户的消息
 * <p>
 * <a href="https://gitee.com/aaronsd/spring-cloud-security/">spring-cloud-security</a>
 *
 * @author <a href="mailto:610195979@qq.com?subject=com.aaron.study.spring-cloud-security">aaron</a>
 * @version 0.0.1
 */
@Log
@RestController
@RequestMapping("/message")
public class MessageController {

	@Autowired
	KeFuFeignClient keFuFeignClient;

	/**
	 * 接收文本消息
	 *
	 * @param fromUser
	 *        customer
	 * @param message
	 *        textMessage
	 * @return textMessage
	 */
	@PostMapping(value = "/text1")
	public TextMessage recvTextMsg(	@RequestParam(name = "fromUser", required = true) String fromUser,
									@RequestParam(name = "message") String message) {
		TextMessage textMessage = new TextMessage();
		textMessage.setFromUser(fromUser);
		textMessage.setMessage(message);
		log.info(textMessage.toString());

		// TODO send to KeFu
		textMessage = keFuFeignClient.send2Kefu(textMessage);

		return textMessage;
	}

	/**
	 * 接收文本消息
	 *
	 * @param textMessage
	 *        textMessage
	 * @return textMessage
	 */
	@PostMapping(value = "/text", produces = "application/json;charset=UTF-8")
	public TextMessage recvTextMsg(@RequestBody TextMessage textMessage) {
		log.info(textMessage.toString());

		// TODO send to KeFu
		textMessage = keFuFeignClient.send2Kefu(textMessage);

		return textMessage;
	}

	/**
	 * 获取服务状态
	 *
	 * @return status
	 */
	@GetMapping(value = "/status")
	public String status() {
		return "ok@" + System.currentTimeMillis();
	}

	/**
	 * 接收客服发送的文本消息
	 *
	 * @param textMessage
	 *        textMessage
	 */
	@PostMapping(value = "/from/kefu", produces = "application/json;charset=UTF-8")
	public void textMessage(@RequestBody TextMessage textMessage) {
		log.info(textMessage.toString());

		sendMessage2Customer(textMessage);
	}

	/**
	 * 模拟发送消息给客户（直接打印日志）
	 *
	 * @param textMessage
	 *        textMessage
	 */
	private void sendMessage2Customer(TextMessage textMessage) {
		log.info("发送消息给客户:" + textMessage);
	}

}
